Dashboard Temp Share Shortlinks Frames API

HTMLify

Painter's partition.cpp
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
    int countStudents(vector<int> &nums, int k) {
    int n = nums.size(); //size of array.
    int students = 1;
    long long pagesStudent = 0;
    for (int i = 0; i < n; i++) {
        if (pagesStudent + nums[i] <= k) {
            //add pages to current student
            pagesStudent += nums[i];
        }
        else {
            //add pages to next student
            students++;
            pagesStudent = nums[i];
        }
    }
    return students;
}
    
    
    
    
    int splitArray(vector<int>& nums, int k) {
    int n = nums.size();
    if (k > n) return -1;

    int low = *max_element(nums.begin(), nums.end());
    int high = accumulate(nums.begin(), nums.end(), 0);
    while (low <= high) {
        int mid = (low + high) / 2;
        int students = countStudents(nums, mid);
        if (students > k) {
            low = mid + 1;
        }
        else {
            high = mid - 1;
        }
    }
    return low;
    }
};